/*
    1. Write a program in C to create and store information in a text file. 
    Test Data :
    Input a sentence for the file : This is the content of the file test.txt.
    Expected Output :

    The file test.txt created successfully...!!
*/

#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN 1024

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p++ = ch;
    }
    *p = '\0';
    return str;
}

int getstrlen(char *str)
{
    int len = 0;
    char *p = str;
    while(*p != '\0'){
        len++;
        p++;
    }
    len++; // '\0'
    return len;
}

int main(void)
{
    FILE *fp;
    char *filename = "test.txt";
    char *str = malloc(MAX_LEN);
    fp = fopen(filename, "w");
    printf("Input a sentence for the file : ");
    getstr(str);
    size_t ret = fwrite(str, getstrlen(str) - 1, 1, fp);
    if(ret){
        printf("The file %s created successfully...!! ", filename);
    }
    fclose(fp);

    return 0;
}